package com.etcfg.etlaunch.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import com.etcfg.etlaunch.ColorConvertor;
import com.etcfg.etlaunch.ETServer;
import com.etcfg.etlaunch.ServerStatus;

public class ServerListCellRenderer implements TableCellRenderer {
	private ImageIcon passwordIcon = new ImageIcon(getClass().getResource(
			"/com/etcfg/etlaunch/ui/icons/emblem-readonly16.png"));
	private ImageIcon pbIcon = new ImageIcon(getClass().getResource(
			"/com/etcfg/etlaunch/ui/icons/pb16.png"));
	private ImageIcon slacIcon = new ImageIcon(getClass().getResource(
			"/com/etcfg/etlaunch/ui/icons/slac16.png"));
	private Color color = new JPanel().getBackground();
	private Font font = new JPanel().getFont();

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		JPanel panel = new JPanel();
		panel.setFont(font);

		panel.setBackground(table.getBackground());
		if (row % 2 == 0) {
			panel.setBackground(table.getBackground());
		} else {
			panel.setBackground(color);
		}

		ETServer etServer = (ETServer) value;
		panel.setLayout(new GridBagLayout());

		if (isSelected) {
			panel.setBackground(table.getSelectionBackground());
		}

		Color colorForeground;
		if (isSelected) {
			colorForeground = table.getSelectionForeground();
		} else {
			colorForeground = table.getForeground();

		}
		GridBagConstraints bc = new GridBagConstraints();
		bc.gridx = 0;
		bc.gridy = 0;
		bc.anchor = GridBagConstraints.WEST;
		bc.weightx = 1;
		bc.gridwidth = 10;

		JLabel serverName = new JLabel(ColorConvertor
				.convertToPlainString(etServer.getServerName()));
		serverName.setFont(font);
		serverName.setForeground(colorForeground);
		panel.add(serverName, bc);
		bc.weightx = 0;
		bc.gridwidth = 1;
		bc.gridy = 1;

		if (etServer.getServerStatus() != null) {
			ServerStatus st = etServer.getServerStatus();
			bc.gridx++;
			JLabel pingLabel = new JLabel(" " + st.getPing() + "ms ");
			pingLabel.setFont(font);
			if (st.getPing() >= 999) {
				pingLabel.setForeground(Color.RED);
			} else {
				pingLabel.setForeground(colorForeground);
			}
			panel.add(pingLabel, bc);
			if (st.isPunkbusterEnabled()) {
				bc.gridx++;
				JLabel pbLabel = new JLabel(pbIcon);
				pbLabel.setForeground(colorForeground);
				panel.add(pbLabel, bc);

			}
			if (st.isPasswordProtected()) {
				bc.gridx++;
				panel.add(new JLabel(passwordIcon), bc);
			}
			if (st.isSlacEnabled()) {
				bc.gridx++;
				panel.add(new JLabel(slacIcon), bc);
			}
			if (st.getMapName() != null) {
				bc.gridx++;
				JLabel mapNameLabel = new JLabel(st.getMapName().trim());
				mapNameLabel.setForeground(colorForeground);
				mapNameLabel.setFont(font);
				panel.add(mapNameLabel);
			}

			if (st.getPlayers() != null) {
				bc.gridx++;

				StringBuilder playersString = new StringBuilder();
				if (st.getPlayers().size() < 10) {
					playersString.append(" ");
				}

				if (st.getBotsplaying() == 0) {
					playersString.append("   ");
					playersString.append(st.getPlayers().size());
				} else {
					if (st.getBotsplaying() < 10) {
						playersString.append(" ");
					}
					if (st.getPlayers().size() - st.getBotsplaying() < 10) {
						playersString.append(" ");
					}
					playersString.append(st.getPlayers().size()
							- st.getBotsplaying() + "+" + st.getBotsplaying());
				}
				playersString.append("/");

				playersString.append(st.getMaxPlayers() - st.getPrivateSlots());
				if (st.getMaxPlayers() < 10) {
					playersString.append(" ");
				}
				if (st.getPrivateSlots() <= 0) {
					playersString.append("    ");
				} else {
					if (st.getPrivateSlots() < 10) {
						playersString.append(" ");
					}
					playersString.append("(");
					playersString.append(st.getPrivateSlots());
					playersString.append(")");

				}

				JLabel serverPlayers = new JLabel(playersString.toString());

				serverPlayers.setFont(font);
				if (isSelected) {

					serverPlayers.setForeground(colorForeground);
				} else {
					if (st.getMaxPlayers() - st.getPrivateSlots() <= st
							.getPlayers().size()) {
						serverPlayers.setForeground(Color.RED);
					} else if (st.getPlayers().size() == 0) {

						serverPlayers.setForeground(Color.GRAY);
					} else {
						serverPlayers.setForeground(new Color(0, 140, 0));
					}
				}
				panel.add(serverPlayers);
			}

		}

		panel.doLayout();
		if (table.getRowHeight(row) != panel.getPreferredSize().height) {
			int height = panel.getPreferredSize().height;
			table.setRowHeight(row, height);
		}

		return panel;
	}
}
